package org.teachertools.web.actions;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.teachertools.data.Eleve;
import org.teachertools.services.EleveService;
import org.teachertools.web.forms.EleveForm;
import org.teachertools.web.forms.EleveFormMapping;

public class EleveAction extends CommonDispatchAction {

    @Override
    protected Map<String, String> getKeyMethodMap() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("button.save", "save");
        map.put("button.delete", "delete");
        map.put("button.search", "search");
        return map;
    }

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        EleveForm eleveForm = (EleveForm) form;
        boolean isUpdate = eleveForm.getIsUpdate().booleanValue();
        EleveService eleveService = this.getServiceLocator().getEleveService();
        if (isUpdate) {
            Eleve loadedEleve = eleveService.get(eleveForm.getId());
            Eleve eleve = EleveFormMapping.mappingFormToDTO(eleveForm, loadedEleve);
            eleveService.update(eleve);
        } else {
            Eleve eleve = new Eleve();
            eleve = EleveFormMapping.mappingFormToDTO(eleveForm, eleve);
            eleveService.save(eleve);
        }

        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lenregistrement")));
        saveMessages(request, actionMessages);
        // ((EleveForm)form).set__ed_isUpdate(Boolean.TRUE);
        eleveForm.reset();
        return mapping.findForward("successSave");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        EleveService eleveService = getServiceLocator().getEleveService();
        Long id = ((EleveForm) form).getId();
        eleveService.deleteById(id);
        ((EleveForm) form).reset();
        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lasuppression")));
        saveMessages(request, actionMessages);
        return mapping.findForward("successDelete");
    }

    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Eleve eleve = EleveFormMapping.mappingFormToDTO((EleveForm) form);
        EleveService eleveService = getServiceLocator().getEleveService();
        Collection<Eleve> list = eleveService.searchByExample(eleve);
        request.setAttribute("RESULTS_LIST", list);
        return mapping.findForward("successSearch");
    }
}
